package com.bob.common.utils;

import com.bob.common.bean.Constant;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;

public class ShellUtil {
    static Log logger = new SystemStreamLog();

    public static void cmdExec(String  shell){
        String [] s ={ "","", ""};
        String os = ShellUtil.getOs();
        switch (os){
            case Constant.SYSTEM_WINDOWS:
                s[0]="cmd";
                s[1]="/C";
                s[2]=shell;
                break;
            default:
                return;
        }

        ShellUtil.cmdExec(s);
    }
    public static void cmdExec(String[] shell){
        try {
            Process process = Runtime.getRuntime().exec(shell);
            String property = System.getProperty("sun.jnu.encoding");
            logger.info(String.format("=== 当前操作系统的编码格式  %s  ", property));
            new Thread(()->{
                try {
                    InputStreamReader inputStreamReader=new InputStreamReader(process.getInputStream(),property);
                    StringBuffer stringBuffer = new StringBuffer();
                    BufferedReader br=new BufferedReader(inputStreamReader);
                    String line=null;
                    //直到读完为止
                    while((line=br.readLine())!=null)
                    {
                        stringBuffer.append(line);
                        stringBuffer.append("\n" );
                        logger.info(String.format("=== %s  ", line));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
            //new Thread(()->{
                try{
                    StringBuffer stringBuffer = new StringBuffer();
                    InputStreamReader errorStreamReader=new InputStreamReader(process.getErrorStream(),property);
                    BufferedReader br=new BufferedReader(errorStreamReader);
                    String line=null;
                    while((line=br.readLine())!=null)
                    {
                        stringBuffer.append(line);
                        stringBuffer.append("\n" );
                        logger.info(String.format("=== %s  ", line));

                    }
                    try {
                        process.waitFor();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            // }).start(); //不用开线程，等待错误输出
        } catch (IOException  e) {
            e.printStackTrace();
        }finally {
        }
    }


    public static String getOs(){
        String os = System.getProperty("os.name");
        logger.info(String.format("=== 当前操作系统  %s  ", os.toLowerCase(Locale.ROOT)));
        if(os.toLowerCase(Locale.ROOT).contains("windows")){
           return Constant.SYSTEM_WINDOWS;
        }else if(os.toLowerCase(Locale.ROOT).contains("linux")){
            return Constant.SYSTEM_LINUX;
        }
        return "unknown";
    }
}
